package com.surelution.projects.crm

import groovy.time.TimeCategory;

class OwnStation extends GasStation {

    static constraints = {
		code unique: true
		manager nullable:true
		manager blank:true
		managerTel nullable:true
		managerTel blank:true
		stationArea nullable:true
		stationArea blank:true
		manageType nullable:true
		manageType blank:true
    }
	/**
	 * 油品年度累计销量、非油品累计收入、油品日均销量、非油品日均销量、年度累计发卡量、卡消比、柴汽比
	 */
	static transients = ['annualSale','noGasAnnualSale','averageSale','noGasAverageSale','annualCardNums','cardProp','gasFuel']
	
	String manager
	String managerTel
	String code
	Level level
	StarLevel star
	TonLevel tonLevel
	StationArea stationArea
	StationManageType manageType
	Boolean isLocked
	Boolean isActived
	
	Double getAnnualSale() {
		Calendar c = Calendar.getInstance()
		int year = c.get(Calendar.YEAR)
		c.clear()
		Date now = new Date()
		c.set(year, 0, 0)
		def firstSight = c.getTime()
		def l = GasSale.createCriteria().get {
			projections {
				sum("saleNumber")
			}
			eq("station", this)
			ge("saleTime", firstSight)
			le("saleTime", now)
		}
		if (l){
			return l/1000
		}
		else 0
	}
	
	Double getNoGasAnnualSale() {
		Calendar c = Calendar.getInstance()
		int year = c.get(Calendar.YEAR)
		c.clear()
		Date now = new Date()
		c.set(year, 0, 0)
		def firstSight = c.getTime()
		def l = NoGasSale.createCriteria().get {
			projections {
				sum("income")
			}
			eq("station", this)
			ge("saleTime", firstSight)
			le("saleTime", now)
		}
		if (l){
			return l
		}
		else 0
	}
	
	Double getAverageSale() {
		Calendar c = Calendar.getInstance()
		int year = c.get(Calendar.YEAR)
		c.clear()
		c.set(year, 0, 0)
		def firstSight = c.getTime()
		Date now = new Date()
		def days = (now-firstSight)
		def l = GasSale.createCriteria().get {
			projections {
				sum("saleNumber")
			}
			eq("station", this)
			ge("saleTime", firstSight)
			le("saleTime", now)
		}
		if (l){
			return l/days/1000
		}
		else 0
	}
	
	Double getNoGasAverageSale() {
		Calendar c = Calendar.getInstance()
		int year = c.get(Calendar.YEAR)
		c.clear()
		c.set(year, 0, 0)
		def firstSight = c.getTime()
		Date now =new Date()
		def days = (now-firstSight)
		def l = NoGasSale.createCriteria().get {
			projections {
				sum("income")
			}
			eq("station", this)
			ge("saleTime", firstSight)
			le("saleTime", now)
		}
		if (l){
			return l/days
		}
		else 0
	}
	
	Double getAnnualCardNums() {
		Calendar c = Calendar.getInstance()
		int year = c.get(Calendar.YEAR)
		c.clear()
		c.set(year, 0, 0)
		def firstSight = c.getTime()
		def l = CardList.createCriteria().get {
			projections {
				count("cardNo")
			}
			eq("station", this)
			ge("assignTime", firstSight)
		}
		if (l){
			return l
		}
		else 0
	}
	
	Double getCardProp() {
		Calendar c = Calendar.getInstance()
		int year = c.get(Calendar.YEAR)
		c.clear()
		c.set(year, 0, 0)
		def firstSight = c.getTime()
		def cardPropNum =0
		Date now =new Date()
		def l = GasSale.createCriteria().list {
			projections {
				sum("icCardAmount")
				sum("actualAmount")
			}
			eq("station", this)
			ge("saleTime", firstSight)
			le("saleTime", now)
		}
		l.each {
			if (it[1]){
				cardPropNum = it[0]/it[1]*100
			}
		}
		return cardPropNum
	}
	
	Double getGasFuel() {
		Calendar c = Calendar.getInstance()
		int year = c.get(Calendar.YEAR)
		c.clear()
		c.set(year, 0, 0)
		def firstSight = c.getTime()
		Date now =new Date()
		def days = (now-firstSight)
		def gasFuelNums = 0
		def categories = GasTypeCategory.list()
		def hql = "select gs.station"
		categories.each {cat->
			hql += ",sum(case when gs.type.category.id = ${cat.id} then gs.saleNumber else 0 end)"
		}
		hql += " from GasSale gs where gs.saleTime < :endDay and gs.saleTime >= :startDay and gs.station = :station"
		def result = GasSale.executeQuery(hql,['startDay':firstSight, 'endDay':now,'station':this])
		result.each {
			if (it[2]) gasFuelNums = it[1]/it[2]
		}
		return gasFuelNums
	}
	
}
